package com.ymttest.business.service;

import java.util.List;
import java.util.Map;
import java.util.Random;

import com.ymatou.shoppingcart.api.parameter.AddCatalogToCartBean;
import com.ymatou.shoppingcart.api.service.AddCatalogToCartCall;
import com.ymt.core.tool.Logger;
import com.ymttest.business.database.ActivityInfo;
import com.ymttest.database.sqlwapper.YmtProductsInActivityWapper;
import com.ymttest.utils.TestDataManager;

/**
 * @author zhouyi
 *
 */
public class ShoppingCartCallServices {
	/**
	 * 给一个uid新增一个购物车数据
	 * 
	 * @see YmtProductsInActivityWapper#selectNewActiveProductInfo()
	 */
	public static Map addCatalogToCart(String uid) throws Exception {
		AddCatalogToCartBean addcatalogtocartbean = new AddCatalogToCartBean();
		AddCatalogToCartCall addcatalogtocartcall = new AddCatalogToCartCall();
		Map productinfo = null;
		try {
			// 随机获取appid 0~6 Unknown=0,洋码头PC端(1)
			// Ymt_PC=1,洋码头WAP(2)Ymt_WAP=2,洋码头APP(3)Ymt_APP=3,神器APP(4)Shangou_APP=4,神器WAP(5)Shangou_WAP=5,神器PC端(6)Shangou_PC=6
			String appid = String.valueOf(new Random().nextInt(7));
			List<Map> apl = ActivityInfo.getActiveProductInfoList();
			productinfo = apl.get(new Random().nextInt(apl.size()));
			// 获取一个catalogId
			String catalogId = (String) productinfo.get("CatalogId");
			// 获取一个sellerId
			String sellerId = String.valueOf(productinfo.get("SellerId"));
			// 获取一个loginId
			String loginId = String.valueOf(productinfo.get("loginId"));
			addcatalogtocartbean.setAppSource(appid);
			addcatalogtocartbean.setCatalogId(catalogId);
			addcatalogtocartbean.setCount(1);
			addcatalogtocartbean.setCountryId("2");
			addcatalogtocartbean.setSellerId(sellerId);
			addcatalogtocartbean.setSellerLoginId(loginId);
			addcatalogtocartbean.setUserId(uid);
			addcatalogtocartcall.setData(false,addcatalogtocartbean);
			addcatalogtocartcall.callService();
		} catch (Exception e) {
			Logger.fail(e);
		}
		return productinfo;
	}

	/**
	 * 
	 * 给一个uid新增多个购物车数据
	 * 
	 * @param uid
	 *            用户账号
	 * @param num
	 *            新增商品数量
	 * @return 添加成功数量
	 * @throws Exception
	 */
	public static int addCatalogsToCart(String uid, int num) throws Exception {
		int count = 0;

		Map productinfo = null;
		try {
			// 随机获取appid 0~6 Unknown=0,洋码头PC端(1)
			// Ymt_PC=1,洋码头WAP(2)Ymt_WAP=2,洋码头APP(3)Ymt_APP=3,神器APP(4)Shangou_APP=4,神器WAP(5)Shangou_WAP=5,神器PC端(6)Shangou_PC=6
			String appid = String.valueOf(new Random().nextInt(7));
			List<Map> apl = ActivityInfo.getActiveProductInfoList();
			if (num > apl.size()) {
				num = apl.size();
			}
			for (int i = 0; i < num; i++) {
				AddCatalogToCartBean addcatalogtocartbean = new AddCatalogToCartBean();
				AddCatalogToCartCall addcatalogtocartcall = new AddCatalogToCartCall();
				productinfo = apl.get(i);
				// 获取一个catalogId
				String catalogId = (String) productinfo.get("CatalogId");
				// 获取一个sellerId
				String sellerId = String.valueOf(productinfo.get("SellerId"));
				// 获取一个loginId
				String loginId = String.valueOf(productinfo.get("loginId"));
				addcatalogtocartbean.setAppSource(appid);
				addcatalogtocartbean.setCatalogId(catalogId);
				addcatalogtocartbean.setCount(1);
				addcatalogtocartbean.setCountryId("2");
				addcatalogtocartbean.setSellerId(sellerId);
				addcatalogtocartbean.setSellerLoginId(loginId);
				addcatalogtocartbean.setUserId(uid);
				addcatalogtocartcall.setData(false,addcatalogtocartbean);
				addcatalogtocartcall.callService();
				if (addcatalogtocartcall.getCode().equals("200")) {
					count++;
				}
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
		return count;
	}

	/**
	 * 使用sellerid 新建一个商品 然后给一个uid新增一个购物车数据，加入的是刚刚新增的商品
	 * 
	 * @param uid
	 * @param m
	 *            productinfo 需要有 scatalogid isellerid loginid
	 * @return Map 返回一个map ProductInActivityInfo
	 * @see YmtProductsInActivityWapper#selectProductInActivityInfoByCatalogId
	 * @throws Exception
	 */
	public static Map addCatalogToCart(String uid, Map productinfo) throws Exception {
		AddCatalogToCartBean addcatalogtocartbean = new AddCatalogToCartBean();
		AddCatalogToCartCall addcatalogtocartcall = new AddCatalogToCartCall();
		Map m = null;
		try {
			// 随机获取appid 0~6 Unknown=0,洋码头PC端(1)
			// Ymt_PC=1,洋码头WAP(2)Ymt_WAP=2,洋码头APP(3)Ymt_APP=3,神器APP(4)Shangou_APP=4,神器WAP(5)Shangou_WAP=5,神器PC端(6)Shangou_PC=6
			String appid = String.valueOf(new Random().nextInt(7));
			// 获取一个catalogId
			String catalogId = (String) productinfo.get("scatalogid");
			// 获取一个sellerId
			String sellerId = String.valueOf(productinfo.get("isellerid"));
			// 获取一个loginId
			String loginId = String.valueOf(productinfo.get("loginid"));
			addcatalogtocartbean.setAppSource(appid);
			addcatalogtocartbean.setCatalogId(catalogId);
			addcatalogtocartbean.setCount(1);
			addcatalogtocartbean.setCountryId("2");
			addcatalogtocartbean.setSellerId(sellerId);
			addcatalogtocartbean.setSellerLoginId(loginId);
			addcatalogtocartbean.setUserId(uid);
			addcatalogtocartcall.setData(false,addcatalogtocartbean);
			addcatalogtocartcall.callService();
			YmtProductsInActivityWapper yp = new YmtProductsInActivityWapper();
			List<Map> list = yp.selectProductInActivityInfoByCatalogId(catalogId);
			if (list.size() > 0) {
				m = list.get(0);
				int redo = 0;
				// 可能出现查询出来的结果没有及时获取最新数据的情况，重试3次查询
				while (null == m.get("fPromotion1") && redo < 3) {
					Logger.debug("not find fPromotion1 ,redo " + redo);
					Thread.sleep(1000);
					list = yp.selectProductInActivityInfoByCatalogId(catalogId);
					m = list.get(0);
					redo++;
				}
			}
			// 商品没有活动的时候会查不到数据
			else {
				throw new Exception(
						"添加商品信息没有从数据库中查询出来 com.ymatou.database.mapping.YmtProductsInActivityMapper.selectProductInActivityInfoByCatalogId,catalogId:"
								+ catalogId);
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
		return m;
	}

	public static void main(String args[]) {
		String gid = TestDataManager.getRandomUUID().toString();

	}
}
